package com.tsf.hbase;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Table;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * hbase访问器
 * 子类可以继承此类实现自己的hbase各项操作或进行扩展
 *
 * @author taoningbo
 * @since 1.0
 */
public abstract class HBaseAccessor implements HBaseClient {

    protected final Logger LOG = LoggerFactory.getLogger(this.getClass());
    protected final HBaseClient hbaseClient;

    public HBaseAccessor(HBaseClient hbaseClient) {
        this.hbaseClient = hbaseClient;
    }

    /**
     * 返回一个{@link HBaseClient}对象实例.
     *
     * @return hbaseClient实例
     */
    public HBaseClient getHbaseClient() {
        return hbaseClient;
    }

    /**
     * 返回一个hbase连接.
     *
     * @return 一个hbase连接
     */
    @Override
    public final Connection connection() {
        return hbaseClient.connection();
    }

    /**
     * 转换一个{@code tableName}.
     *
     * @param tableName 表名称
     * @return TableName
     */
    @Override
    public final TableName getTableName(String tableName) {
        return hbaseClient.getTableName(tableName);
    }

    /**
     * 根据{@code tableName}获取一个{@code Table}实例.
     *
     * @param tableName 表名称
     * @return Table
     */
    @Override
    public final Table getTable(String tableName) {
        return hbaseClient.getTable(tableName);
    }

    protected void close(Table table) {
        try {
            table.close();
        } catch (IOException e) {
            LOG.warn("Closed hbase table error. {}", e.getMessage());
        }
    }
}
